<?php
/*
 * @Description: 方糖消息推送
 * @Version: 1.0
 * @Author: beiwangshan
 * @Date: 2023-03-07 23:38:01
 * @LastEditors: zenghao
 * @LastEditTime: 2023-03-13 11:16:59
 */

function sc_send_msg($text, $desp = '', $key = '[SENDKEY]')
{
    $api_url = 'https://sctapi.ftqq.com/' . $key . '.send';
    // $postdata = http_build_query(array('text' => $text, 'desp' => $desp));
    // $opts = array('http' =>
    // array(
    //     'method'  => 'POST',
    //     'header'  => 'Content-type: application/x-www-form-urlencoded',
    //     'content' => $postdata
    // ));
    $headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded'
    );
    $post_data = array(
        'text' => $text,
        'desp' => $desp
    );
    $result = EL_HTTP::doPost($api_url, array(
        'headers' => $headers,
        'body' => $post_data,
        'timeout' => 30,
    ));

    // $context  = stream_context_create($opts);
    return $result;
}

function sc_send_comment($comment_id)
{
    $text = '恭喜主人，又有小伙伴来留言了！';
    $comment = get_comment($comment_id);
    $desp = $comment->comment_content;
    $key = '你申请的SCKEY';
    sc_send_msg($text, $desp, $key);
}
// add_action('comment_post', 'sc_send', 19, 2);